package com.ss.android.f.a;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.Signature;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.bytedance.common.utility.Logger;
import com.ss.android.common.applog.NetUtil;
import com.ss.android.common.AppContext;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.f.b.RegisterServiceController;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;

public final class RegistrationHeaderHelper {
    private static String a;
    private static AppContext b;
    private static String c;
    private static int d;
    private static int e;
    private static String f;
    private static String g;
    private static ConcurrentHashMap h = new ConcurrentHashMap();
    private static JSONObject i;
    private static String j;
    private static String k;
    private static String l;
    private static String m;
    private static final Object n = new Object();
    private static RegisterServiceController o;
    private static ConcurrentHashMap p;

    public static boolean a(Context arg13, JSONObject arg14) {
        JSONArray v4_1;
        int v1_3;
        int v8_1;
        String v0_6;
        Object v0_5;
        String v8;
        int v5_1;
        String v1_1;
        int v0_4;
        String v5 = null;
        int v12 = 4;
        Object v1 = null;
        boolean v2 = true;
        int v3 = 0;
        String v4 = null;
        HashMap v6 = new HashMap();
        if (i != null) {
            v1 = n;
            synchronized (v1) {
                a(i, arg14);
                return v2;
            }
        }

        JSONObject v9 = new JSONObject();
        try {
            v5 = arg13.getPackageName();
        } catch (Exception v0_1) {

        }

        try {
            Bundle v0_2 = arg13.getPackageManager().getApplicationInfo(v5, 128).metaData;
            if (a == null || a.length() <= 0) {
                ((Map) v6).put("channel", v0_2.getString("UMENG_CHANNEL"));
            } else {
                ((Map) v6).put("channel", a);
            }
            ((Map) v6).put("appkey", v0_2.getString("UMENG_APPKEY"));
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        }
        try {
            ((Map) v6).put("package", arg13.getPackageName());
            if (b != null) {
                c = b.getVersion();
            }

            if (!android.support.design.a.isTextEmpty((String) null)) {
                c = null;
            }

            ((Map) v6).put("app_version", c);
            if (b != null) {
                d = b.getVersionCode();
            }

            PackageInfo v0_3 = arg13.getPackageManager().getPackageInfo(v5, 0);
            if (v0_3.applicationInfo != null) {
                v0_4 = v0_3.applicationInfo.labelRes;
                if (v0_4 > 0) {
                    v9.put("display_name", arg13.getString(v0_4));
                }
            }

            if (b != null) {
                v0_4 = b.getUpdateVersionCode();
                if (v0_4 > 0) {
                    v9.put("update_version_code", v0_4);
                }

                v0_4 = b.getManifestVersion();
                if (v0_4 <= 0) {

                } else {
                    v9.put("manifest_version_code", v0_4);
                }
            }
        } catch (Exception v0_1) {
            label_129:
            v1_1 = "RegistrationHeaderHelper";
            Logger.w(v1_1, "init exception: " + v0_1);
            return false;
        }

        String[] v7 = new String[v12];
        v7[0] = "channel";
        v7[1] = "appkey";
        v7[2] = "package";
        v7[3] = "app_version";
        try {
            if (e == 0 && b != null) {
                e = b.getAid();
            }

            v9.put("aid", e);
            v5_1 = 0;
            while (true) {
                if (v5_1 >= v12) {
                    break;
                }

                v8 = v7[v5_1];
                v0_5 = ((Map) v6).get(v8);
                if (android.support.design.a.isTextEmpty(((String) v0_5))) {
                    Logger.w("RegistrationHeaderHelper", "init fail empty field: " + v8);
                    if (!"appkey".equals(v8)) {
                        return false;
                    }
                } else {
                    v9.put(v8, v0_5);
                }
                ++v5_1;
                continue;
            }
            v9.put("version_code", d);
            v9.put("sdk_version", 201);
            v9.put("os", "Android");
            v9.put("os_version", Build.VERSION.RELEASE);
            v9.put("os_api", Build.VERSION.SDK_INT);
            v9.put("device_model", Build.MODEL);
            v9.put("device_brand", Build.BRAND);
            v9.put("device_manufacturer", Build.MANUFACTURER);
            v9.put("cpu_abi", Build.CPU_ABI);
            v9.put("build_serial", Build.SERIAL);
            v0_6 = f;
            if (v0_6 == null) {
                v0_6 = "";
            }
            v9.put("release_build", v0_6);
            DisplayMetrics v5_2 = arg13.getResources().getDisplayMetrics();
            v0_4 = v5_2.densityDpi;
            v9.put("density_dpi", v0_4);
            switch (v0_4) {
                case 120: {
                    v0_6 = "ldpi";
                    break;
                }
                case 240: {
                    v0_6 = "hdpi";
                    break;
                }
                case 320: {
                    v0_6 = "xhdpi";
                    break;
                }
                default: {
                    v0_6 = "mdpi";
                    break;
                }
            }

            v9.put("display_density", v0_6);
            v9.put("resolution", v5_2.heightPixels + "x" + v5_2.widthPixels);
        } catch (Exception v0_1) {
        }

        try {
            v0_6 = arg13.getResources().getConfiguration().locale.getLanguage();
            if (!android.support.design.a.isTextEmpty(v0_6)) {
                v9.put("language", v0_6);
            }

            v0_6 = android.support.design.a.d(arg13);
            if (!android.support.design.a.isTextEmpty(v0_6)) {
                v9.put("mc", v0_6);
                g = v0_6;
            }

            v0_4 = TimeZone.getDefault().getRawOffset() / 3600000;
            if (v0_4 < (((int) 0))) {
                v0_4 = ((int) 0);
            }

            if (v0_4 > 12) {
                v0_4 = 12;
            }

            v9.put("timezone", v0_4);
            v9.put("access", android.support.design.a.doGetNetworkAccessType(android.support.design.a.getNetworkType(arg13)));
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        }

        try {
            if (android.support.design.a.isTextEmpty(NetUtil.b())) {
            }else {
                v9.put("aliyun_uuid", NetUtil.b());
            }
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        }

        try {
            if (!h.isEmpty()) {
                Iterator v5_3 = h.entrySet().iterator();
                while (v5_3.hasNext()) {
                    v0_5 = v5_3.next();
                    if (v0_5 == null) {
                        continue;
                    }

                    v9.put((String) ((Entry) v0_5).getKey(), ((Map.Entry) v0_5).getValue());
                }
            }

            v9.put("not_request_sender", 0);
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        }

        try {
            v0_5 = arg13.getSystemService(Context.TELEPHONY_SERVICE);
            v1_1 = ((TelephonyManager) v0_5).getNetworkOperatorName();
            v0_6 = ((TelephonyManager) v0_5).getNetworkOperator();
            if (!android.support.design.a.isTextEmpty(v1_1)) {
                v9.put("carrier", v1_1);
            }

            if (android.support.design.a.isTextEmpty(v0_6)) {
            }else {
                v9.put("mcc_mnc", v0_6);
            }
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        }

        StringBuilder v0_7 = new StringBuilder();
        try {
            if (ToolUtils.isMiui()) {
                v0_7.append("MIUI-");
            } else if (ToolUtils.c()) {
                v0_7.append("FLYME-");
            } else {
                v1_1 = ToolUtils.getEmuiInfo();
                if (ToolUtils.b(v1_1)) {
                    v0_7.append("EMUI-");
                }

                if (TextUtils.isEmpty(((CharSequence) v1_1))) {

                }else {
                    v0_7.append(v1_1).append("-");
                }
            }

            v0_7.append(Build.VERSION.INCREMENTAL);
            if (v0_7.length() <= 0) {
            }else {
                l = v0_7.toString();
                v9.put("rom", l);
            }
        } catch (Throwable v0) {
        }

        try {
            v0_6 = com.ss.android.f.c.a.a();
            if (android.support.design.a.isTextEmpty(v0_6)) {
            }else {
                v9.put("rom_version", v0_6);
            }
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
        v0_6 = a(arg13);
        if (v0_6 != null) {
            try {
                v9.put("sig_hash", v0_6);
            } catch (JSONException v0_8) {
                v0_8.printStackTrace();
            }
        }

        SharedPreferences v10 = arg13.getSharedPreferences(com.ss.android.f.a.a.a(), 0);
        v0_6 = v10.getString("mac_addr", v4);
        String v6_1 = v10.getString("google_aid", v4);
        v5 = v10.getString("app_language", v4);
        v1_1 = v10.getString("app_region", v4);
        if (android.support.design.a.isTextEmpty(v0_6)) {
            if (!android.support.design.a.isTextEmpty(g)) {
                v0_4 = 1;
            }else{
                v0_4 = 0;
            }
        } else if (android.support.design.a.isTextEmpty(g)) {
            g = v0_6;
            try {
                v9.put("mc", g);
                v0_4 = 0;
            } catch (JSONException v0_8) {
                v0_8.printStackTrace();
                v0_4 = 0;
            }
        } else if (!v0_6.equals(g)) {
            v0_4 = 1;
        }else{
            v0_4 = 0;
        }
        String v7_1 = null;
        try {
            if (android.support.design.a.isTextEmpty(v7_1)) {
                v7_1 = v6_1;
                v8_1 = 0;
            } else if (!((String)null).equals(v6_1)) {
                v7_1 = v4;
                v8_1 = 1;
            } else {
                v7_1 = v6_1;
                v8_1 = 0;
            }
            if (!android.support.design.a.isTextEmpty(v7_1)) {
                v9.put("google_aid", v7_1);
            }

            if (android.support.design.a.isTextEmpty((String) null)) {
                v6_1 = v5;
                v5_1 = 0;
            } else if (!((String)null).equals(v5)) {
                v5_1 = 1;
                v6_1 = v4;
            } else {
                v6_1 = v5;
                v5_1 = 0;
            }
            if (!android.support.design.a.isTextEmpty(v6_1)) {
                v9.put("app_language", v6_1);
            }

            if (android.support.design.a.isTextEmpty((String) null)) {
                v4 = v1_1;
                v1_3 = 0;
            } else if (!((String)null).equals(v1_1)) {
                v1_3 = 1;
            } else {
                v4 = v1_1;
                v1_3 = 0;
            }
            if (!android.support.design.a.isTextEmpty(v4)) {
                v9.put("app_region", v4);
            }

            SharedPreferences.Editor v11 = v10.edit();
            if (v8_1 != 0) {
                v11.putString("google_aid", v7_1);
            }

            if (v5_1 != 0) {
                v11.putString("app_language", v6_1);
            }

            if (v1_3 != 0) {
                v11.putString("app_region", v4);
            }

            if (v5_1 == 0 && v1_3 == 0 && v8_1 == 0) {
            }else {
                v11.commit();
            }
        } catch (Throwable v1_2) {

        }

        if (v0_4 != 0) {
            SharedPreferences.Editor v0_9 = v10.edit();
            v0_9.putString("mac_addr", g);
            v0_9.commit();
        }

        m = v10.getString("app_track", "");
        try {
            if (android.support.design.a.isTextEmpty(m)) {
            }else {
                v9.put("app_track", new JSONObject(m));
            }
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
        if (o == null) {

        }else {
            try {
                v0_6 = o.a();
                if (!android.support.design.a.isTextEmpty(v0_6)) {
                    v9.put("device_id", v0_6);
                }

                v0_6 = o.a.a();
                if (!android.support.design.a.isTextEmpty(v0_6)) {
                    v9.put("openudid", v0_6);
                }

                v0_6 = o.d;
                if (!android.support.design.a.isTextEmpty(v0_6)) {
                    v9.put("install_id", v0_6);
                }

                v0_6 = o.a.e();
                if (!android.support.design.a.isTextEmpty(v0_6)) {
                    v9.put("udid", v0_6);
                }

                v0_6 = o.a.b();
                if (!android.support.design.a.isTextEmpty(v0_6)) {
                    v9.put("clientudid", v0_6);
                }

                v0_6 = o.a.c();
                if (!android.support.design.a.isTextEmpty(v0_6)) {
                    v9.put("serial_number", v0_6);
                }

                String[] v1_4 = o.a.d();
                if (v1_4 == null) {
                } else {
                    v4_1 = new JSONArray();
                    v5_1 = v1_4.length;
                    while (true) {
                        if (v3 >= v5_1) {
                            break;
                        }
                        v0_6 = v1_4[v3];
                        try {
                            v4_1.put(new JSONObject().put("sim_serial_number", v0_6));
                        } catch (JSONException v0_8) {
                            v0_8.printStackTrace();
                        }
                        ++v3;
                        continue;
                    }
                    v9.put("sim_serial_number", v4_1);
                }
            } catch (Exception v0_1) {
                label_493:
                v0_1.printStackTrace();
            }
        }

        label_494:
        if (p == null) {

        } else {
            Iterator v3_1 = p.entrySet().iterator();
            while (v3_1.hasNext()) {
                v0_5 = v3_1.next();
                try {
                    if (((Map.Entry) v0_5).getValue() == null) {
                        continue;
                    }
                    v9.put((String) ((Entry) v0_5).getKey(), ((Map.Entry) v0_5).getValue());
                } catch (JSONException v0_8) {
                    v0_8.printStackTrace();
                }
            }
        }

        try {
            v0_6 = Locale.getDefault().getCountry();
            if (!android.support.design.a.isTextEmpty(v0_6)) {
                v9.put("region", v0_6);
            }

            v0_6 = Calendar.getInstance().getTimeZone().getID();
            if (!android.support.design.a.isTextEmpty(v0_6)) {
                v9.put("tz_name", v0_6);
            }

            v9.put("tz_offset", Calendar.getInstance().getTimeZone().getOffset(System.currentTimeMillis() / 1000));
            v0_6 = ((TelephonyManager) arg13.getSystemService(Context.TELEPHONY_SERVICE)).getSimCountryIso();
            if (android.support.design.a.isTextEmpty(v0_6)) {

            } else {
                v9.put("sim_region", v0_6);
            }
        } catch (Throwable v0) {

        }

        v1 = n;
        synchronized (v1) {
            i = v9;
            a(v9, arg14);
        }
        return v2;
    }

    public static void a(String arg0) {
        f = arg0;
    }

    public static String a() {
        return null;
    }

    public static void a(Context arg2, String arg3) {
        if (!android.support.design.a.isTextEmpty(arg3) && !arg3.equals(k)) {
            k = arg3;
            SharedPreferences.Editor v0 = arg2.getSharedPreferences(com.ss.android.f.a.a.a(), 0).edit();
            v0.putString("user_agent", arg3);
            v0.commit();
        }
    }

    public static String a(Context arg4) {
        if ((android.support.design.a.isTextEmpty(j)) && arg4 != null) {
            try {
                PackageInfo v0_1 = arg4.getPackageManager().getPackageInfo(arg4.getPackageName(), 64);
                if (v0_1 == null || v0_1.signatures == null || v0_1.signatures.length <= 0) {
                    String v0_2 = j;
                    return v0_2;
                }

                Signature v0_3 = v0_1.signatures[0];
                if (v0_3 == null) {
                    return j;
                }

                j = com.bytedance.common.utility.aa.a(v0_3.toByteArray());
            } catch (Exception v0) {
                Logger.w("RegistrationHeaderHelper", "failed to inst package sianature: " + v0);
            }
        }

        return j;
    }

    @Deprecated
    public static void a(int arg0) {
        e = arg0;
    }

    public static void a(AppContext arg0) {
        b = arg0;
    }

    public static void a(RegisterServiceController arg0) {
        o = arg0;
    }

    public static void a(String arg3, Object arg4) {
        if (arg4 != null) {
            if (Logger.debug()) {
                Logger.d("RegistrationHeaderHelper", "put header : key = " + arg3 + ", val = " + arg4.toString());
            }

            if (p == null) {
                p = new ConcurrentHashMap();
            }

            p.put(arg3, arg4);
        }
    }

    private static void a(JSONObject arg3, JSONObject arg4) {
        Iterator v1 = arg3.keys();
        while (v1.hasNext()) {
            Object v0 = v1.next();
            try {
                arg4.put(((String) v0), arg3.opt(((String) v0)));
            } catch (JSONException v0_1) {
                v0_1.printStackTrace();
            }
        }
    }

    public static String b(Context arg3) {
        if (android.support.design.a.isTextEmpty(k)) {
            k = arg3.getSharedPreferences(com.ss.android.f.a.a.a(), 0).getString("user_agent", null);
        }

        return k;
    }

    public static int b() {
        return d;
    }

    public static void b(Context arg2, String arg3) {
        try {
            SharedPreferences.Editor v0_1 = arg2.getSharedPreferences(com.ss.android.f.a.a.a(), 0).edit();
            v0_1.putString("app_track", arg3);
            v0_1.commit();
        } catch (Throwable v0) {
        }
    }

    public static void b(String arg0) {
        a = arg0;
    }

    public static String c() {
        return c;
    }
}

